package src.data;

import java.util.HashSet;
import java.util.Set;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import src.domain.MailBox;
import src.domain.OutgoingMessage;


public class OutgoingMessageDAO {
	
	private HibernateProvider provider;

	private static OutgoingMessageDAO instance = null;

	public static OutgoingMessageDAO getInstance() {
		if (instance == null)
			instance = new OutgoingMessageDAO();
		return instance;
	}

	public OutgoingMessage getByID(OutgoingMessage outgoingMessage){
		Criteria criteria = provider.getSession().createCriteria(OutgoingMessage.class).add(Restrictions.like("id",outgoingMessage.getId()));
		return (OutgoingMessage) criteria.uniqueResult();	
	}
	
	private OutgoingMessageDAO() {
		this.provider = HibernateProvider.getInstance();

	}
	
	public void save(OutgoingMessage outgoingMessage) {

		provider.getSession().save(outgoingMessage);

	}
	
	@SuppressWarnings("unchecked")
	public Set<OutgoingMessage> getOutgoingMessages(MailBox mailBox) {

		Criteria criteria = provider.getSession().createCriteria(OutgoingMessage.class).add(Restrictions.like("sender", mailBox));
		Set<OutgoingMessage> set = new HashSet<OutgoingMessage>();
		set.addAll(criteria.list());
		return set;

	}
	
	

}
